package com.yang.wechat.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.Scanner;

/**
 * Created with Administrator
 * DATE:2017/3/8
 * Time:17:57
 */
public class Common {


    public static void ShowMessage(Object o) {
        System.out.println(o);
    }


    /**
     * 获取文件内容
     *
     * @param fileName 文件名  //   String path = new AutoClubJob().getFile("conf/AutoClubConfig.xml");
     * @return 文件内容
     */
    public String getFile(String fileName) {
        StringBuilder builder = new StringBuilder("");
        ClassLoader classLoader = getClass().getClassLoader();
        try {
            URL url = classLoader.getResource(fileName);
            if (url != null) {
                File file = new File(url.getFile());

                Scanner scanner = new Scanner(file);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    builder.append(line).append("\n");
                }
                scanner.close();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }


    /**
     * 根据名称获取配置信息
     *
     * @param name
     * @return
     */
    public static String getConfigDB(String name) {
        String result = "";
        if (!name.equals("")) {
            result = Common.getConfig("/prop/db.properties", name);
        }
        return result;
    }

    /**
     * 根据名称获取配置信息
     *
     * @param name name
     * @return key
     */
    public static String getConfigCommon(String name) {
        String result = "";
        if (!name.equals("")) {
            result = Common.getConfig("/prop/common.properties", name);
        }
        return result;
    }

    /**
     * 根据名称获取配置信息
     *
     * @param file 文件路径
     * @param name 节点名称
     * @return
     */
    private static String getConfig(String file, String name) {
        String result = "";
        if (!file.equals("") && !name.equals("")) {
            try {
                Properties pro = new Properties();
                FileInputStream in = new FileInputStream(Common.class.getResource(file).getPath());
                pro.load(in);
                in.close();
                result = pro.getProperty(name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
